package com.example.finallyjob.Controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.finallyjob.entity.Article;
import com.example.finallyjob.entity.User;
import com.example.finallyjob.service.IndexService;
import com.example.finallyjob.tool.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(description = "网站内容")
@RestController
public class IndexController {
    @Autowired
    IndexService indexService;

    @ApiOperation("查找最新的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页数", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页有多少条数据", required = true, dataType = "Int")
    })
    @GetMapping("/NewText/{page}/{size}")
    public List<Article> NewText(@PathVariable (name = "page") int page,
                                 @PathVariable (name = "size") int size) {
        return indexService.NewText(page,size);
    }

    @ApiOperation("查找最新的视频")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页数",  dataType = "Int"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页有多少条数据",  dataType = "Int")
    })
    @GetMapping("/NewVideo/{page}/{size}")
    public List<Article> NewVideo(@PathVariable (name = "page") int page,
                                  @PathVariable (name = "size") int size){
        return indexService.NewVideo(page,size);
    }

    @ApiOperation("查找最热门的文章")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页数", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页有多少条数据", required = true, dataType = "Int")
    })
    @GetMapping("/HotText/{page}/{size}")
    public List<Article> HotText(@PathVariable (name = "page") int page,
                                 @PathVariable (name = "size") int size){
        return indexService.HotText(page,size);
    }

    @ApiOperation("查找最热门的视频")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页数", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页有多少条数据", required = true, dataType = "Int")
    })
    @GetMapping("/HotVideo/{page}/{size}")
    public List<Article>  HotVideo(@PathVariable (name = "page") int page,
                                   @PathVariable (name = "size") int size){
        return indexService.HotVideo(page, size);
    }

//    @ApiOperation(value = "按照题目名查询作品")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "body", name = "str", value = "name、page、size", required = true, dataType = "String")
//    })
//    @PostMapping("/selectT")
//    public List<Article>  selectT(@RequestBody String str, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        JSONObject jsonObject= JSON.parseObject(str);
//        String name=jsonObject.getString("name");
//        Integer page=jsonObject.getObject("page",Integer.class);
//        Integer size=jsonObject.getObject("size",Integer.class);
//        return indexService.selectT(name, page, size);
//    }

//    @ApiOperation(value = "按照作者名查询用户",notes = "返回用户基本信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "body", name = "str", value = "name、page、size", required = true, dataType = "String")
//    })
//    @PostMapping("/selectU")
//    public List<User>  selectU(@RequestBody String str){
//        JSONObject jsonObject= JSON.parseObject(str);
//        String name=jsonObject.getString("name");
//        Integer page=jsonObject.getObject("page",Integer.class);
//        Integer size=jsonObject.getObject("size",Integer.class);
//        return indexService.selectU(name,page,size);
//    }

    @ApiOperation("按照作品类别查询作品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type", value = "作品类型（text||video）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "kind", value = "作品类别（）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "page", value = "当前页数", required = true,dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "每页有多少条数据", required = true,dataType = "Int")
    })
    @PostMapping("/TextZone")
    public List<Article> TextZone(@RequestParam(name = "type") String type,
                                  @RequestParam(name = "kind")String kind,
                                  @RequestParam(name = "page")int page,
                                  @RequestParam(name = "size")int size){
        return indexService.TextZone(type, kind, page, size);
    }

    @ApiOperation(value = "按照用户id查找用户信息",notes = "返回用户的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "用户id", required = true,dataType = "Int")
    })
    @PostMapping("/userDetail/{id}")
    public User userDetail(@PathVariable("id") int id) throws Exception {
        return indexService.userDetail(id);
    }
}
